<template>
  <div class="w-card content-box">
    <div class="title-box">
      <span class="title">商品购买配置</span>
      <div class="title-btns">
        <el-button @click="importHandle">导入</el-button>
        <el-button type="primary" icon="Plus" @click="addControl">添加配置</el-button>
      </div>
    </div>
    <div class="search-box">
      <div class="search-item">
        <span class="item-label">商品：</span>
        <el-select style="width:100%;" v-model="searchForm.goodsId" clearable filterable>
          <el-option v-for="item in goodsOptions" :key="item.goodsId" :label="item.goodsName" :value="item.goodsId" />
        </el-select>
      </div>
      <el-button style="margin-left: 15px;" type="primary" @click="searchHandle">查询</el-button>
      <el-button @click="resetHandle">重置</el-button>
    </div>
    <div class="table-box">
      <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="goodsName" label="商品名称" align="center">
          <template v-slot="scope">
            {{ scope.row.goodsName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="num" label="限制数量" align="center">
          <template v-slot="scope">
            {{ scope.row.num || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="day" label="限制天数" align="center">
          <template v-slot="scope">
            {{ scope.row.day || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="operations" label="操作" align="center" width="120" fixed="right">
          <template v-slot="scope">
            <el-link style="font-size:14px;" type="danger" @click="deleteHandle(scope.row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-footer">
        <el-pagination style="margin-left: auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>

    <add-control ref="addControlRef" @submitSuccess="submitSuccess" />
    <in-out-dialog ref="inOutDialogRef" @dialogClose="inOutDialogClose" />
    <confirm-pass ref="confirmPassRef" @verification="deleteSubmit" />
  </div>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { getDateTime, getWindowWidthAndHeight, successDeal, warningDeal } from '@/utils/utils'
import addControl from './components/addControl.vue'
import inOutDialog from './components/inOutDialog.vue'
import confirmPass from '@/components/confirmPass.vue'
import api from './api'
export default {
  components: {
    addControl,
    inOutDialog,
    confirmPass,
  },
  setup() {
    const addControlRef = ref(null)
    const confirmPassRef = ref(null)
    const inOutDialogRef = ref(null)

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      tableData: [],
      searchForm: {
        orderBy: 'id desc'
      },

      goodsOptions: [],
      handleData: {},
    })

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      getTableData()
      getGoodsList()
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm
      }
      api.goodsOrderNumberList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data?.list || []
          state.total = res.data?.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        orderBy: 'id desc'
      }
      searchHandle()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function getGoodsList() {
      api.goodsInfoAll().then(res => {
        if (res.code == '200') {
          state.goodsOptions = res.data || []
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addControl() {
      addControlRef.value.dialogShow()
    }

    function importHandle() {
      inOutDialogRef.value.dialogShow()
    }

    function submitSuccess() {
      searchHandle()
    }

    function deleteHandle(data) {
      state.handleData = data
      const title = '删除提示'
      const msg = '确认要删除该配置项吗？'
      confirmPassRef.value.dialogShow(title, msg, false)
    }

    function deleteSubmit() {
      const { id } = state.handleData
      const obj = {
        sysOrderId: id,
      }
      api.goodsOrderNumberDelete(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function inOutDialogClose() {
      searchHandle()
    }

    return {
      ...toRefs(state),
      addControlRef,
      confirmPassRef,
      inOutDialogRef,
      addControl,
      searchHandle,
      resetHandle,
      handleSizeChange,
      handleCurrentChange,
      getDateTime,
      importHandle,
      submitSuccess,
      deleteHandle,
      deleteSubmit,
      inOutDialogClose,
    }
  }
}
</script>

<style lang="scss" scoped>
.content-box {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 20px;
  .title-box {
    display: flex;
    align-items: center;
    .title {
      font-size: 16px;
      font-weight: bold;
    }
    .title-btns {
      margin-left: auto;
    }
  }
  .search-box {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding: 20px 0;
    .search-item {
      display: flex;
      align-items: center;
      .item-label {
        flex-shrink: 0;
        font-size: 14px;
      }
    }
  }
  .table-box {
    .table-footer {
      display: flex;
      align-items: center;
      margin-top: 20px;
    }
  }
}
</style>